package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class extends a composite and will allow the
 * users of the system to perform actions based on their
 * user type. Actually, this represents the main page
 * after the login has been performed
 * 
 * @author Amigos do Noddy
 */
public class Default extends Composite {

	private static ArrayList SessionVar = new ArrayList();
	private static ArrayList SessionVal = new ArrayList();
	final static HorizontalPanel hpData = new HorizontalPanel();
	
	/**
	 * Gets the value of a session variable
	 * 
	 * @param name Name of the session variable
	 * @return Value of the session variable
	 */
	public static Object getSession(String name)
	{
		for(int i = 0; i< SessionVar.size(); i++)
		{
			if (((String)SessionVar.get(i)).equals(name))
				return SessionVal.get(i);
		}
		return null;
	}
	
	/**
	 * Sets the value of a session variable
	 * 
	 * @param name Name of the session variable
	 * @param value Value of the session variable
	 */
	public static void setSession(String name, Object value)
	{
		int idx = -1;
		for(int i = 0; i< SessionVar.size(); i++)
			if (((String)SessionVar.get(i)).equals(name))
			{
				idx = i;
				break;
			}
		
		if(idx == -1)
		{
			SessionVar.add(name);
			SessionVal.add(value);
		}
		else
			SessionVal.set(idx, value);
	}
	
	/**
	 * Clears the session variables
	 */
	public static void clearSession()
	{
		SessionVar.clear();
		SessionVal.clear();
	}
	
	/**
	 * Class constructor
	 * 
	 * Creates the widgets on the window
	 */
	public Default() {

		final Label lblSeccao = new Label("");
		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSize("100%", "100%");

		final DockPanel dockPanel = new DockPanel();
		verticalPanel.add(dockPanel);
		dockPanel.setWidth("100%");

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		dockPanel.add(verticalPanel_1, DockPanel.NORTH);
		verticalPanel_1.setWidth("100%");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("100%");
		horizontalPanel_1.setStyleName("loginPanel");
		horizontalPanel_1.setSpacing(8);

		final DockPanel dockPanel_1 = new DockPanel();
		horizontalPanel_1.add(dockPanel_1);
		dockPanel_1.setWidth("100%");
		dockPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		final Image image = new Image();
		dockPanel_1.add(image, DockPanel.WEST);
		image.setUrl("images/logo.png");

		final VerticalPanel verticalPanel_2 = new VerticalPanel();
		dockPanel_1.add(verticalPanel_2, DockPanel.EAST);
		verticalPanel_2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		verticalPanel_2.setWidth("100%");

		final HorizontalPanel horizontalPanel_6 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_6);

		final Label lblUserInfo = new Label("welcome");
		horizontalPanel_6.add(lblUserInfo);
		lblUserInfo.setStyleName("label");
		String username = getSession("Username").toString();
		int usertype = ((Integer)getSession("Tipo")).intValue();
		String[] types = {"Utilizador", "Assistente", "Administrador", "CEO"};
		lblUserInfo.setText(username + " (" + types[usertype-1] + ")");
		
		final HorizontalPanel horizontalPanel_7 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_7);
		horizontalPanel_7.setSpacing(4);

		final Button btnEditarConta = new Button();
		horizontalPanel_7.add(btnEditarConta);
		btnEditarConta.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				hpData.clear();
				hpData.add(new LoginAccount());
				lblSeccao.setText("Editar conta");
			}
		});
		btnEditarConta.setStyleName("menu_button");
		btnEditarConta.setText("» conta");

		final Button btnSair = new Button();
		horizontalPanel_7.add(btnSair);
		btnSair.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				// limpar as variaveis de sessao e voltar para a pagina de login
				clearSession();
				Index.reload(); // ver isto melhor
			}
		});
		btnSair.setStyleName("menu_button");
		btnSair.setText("» Sair");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_2);
		horizontalPanel_2.setStyleName("gwt-MenuBar");
		horizontalPanel_2.setWidth("100%");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel_2.add(horizontalPanel);

		
		final Label menuLabel = new Label("Menu");
		horizontalPanel.add(menuLabel);
		menuLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		menuLabel.setStyleName("MenuTitle");
		menuLabel.setWidth("50");

		{
			final MenuBar menuBarInic = new MenuBar();
			horizontalPanel.add(menuBarInic);

			final MenuItem mInicio = menuBarInic.addItem("Início", new Command() {
				public void execute() {
					lblSeccao.setText("Início");
					hpData.clear();
					hpData.add(new Inicio());
				}
			});
			mInicio.setStyleName("Mygwt-MenuItem");
		}

		{
			final MenuBar menuBarAssis = new MenuBar();
			horizontalPanel.add(menuBarAssis);

			final MenuBar menuBar_1 = new MenuBar(true);

			if(getSession("Tipo").toString().equals("1"))//User
			{
				final MenuItem mAssistencia1 = menuBar_1.addItem("» Adicionar Pedido", new Command() {
					public void execute() {
						lblSeccao.setText("Assistência :: Adicionar Pedido");
						hpData.clear();
						hpData.add(new TicketAdd());
					}
				});
				mAssistencia1.setStyleName("Mygwt-MenuSubItem");
			}
			if(new Integer(getSession("Tipo").toString()).intValue() < 3)//User and Assistant
			{
				final MenuItem mAssistencia2 = menuBar_1.addItem("» Ver Pedidos", new Command() {
					public void execute() {
						hpData.clear();
						if(getSession("Tipo").toString().equals("1"))
						{
							System.out.println("Default chamou Tickets View User");
							hpData.add(new TicketsViewUser());
							lblSeccao.setText("Assistência :: Ver Pedidos");
						}
						else if(getSession("Tipo").toString().equals("2"))
						{
							hpData.add(new TicketsView());
							lblSeccao.setText("Assistência :: Ver Pedidos");
						}
					}
				});
				mAssistencia2.setStyleName("Mygwt-MenuSubItem");
			}

			final MenuItem mAssistencia = menuBarAssis.addItem("Assistência", menuBar_1);
			if(new Integer(getSession("Tipo").toString()).intValue() > 1)//Assistant and adm
			{
					final MenuItem mAssistencia3 = menuBar_1.addItem("» Ver Pedidos Pendentes", new Command(){
					public void execute() {
						hpData.clear();
						hpData.add(new TicketsViewPending());
						lblSeccao.setText("Assistência :: Ver Pedidos Pendentes");
					}
				});
				mAssistencia3.setStyleName("Mygwt-MenuSubItem");
			}
			mAssistencia.setStyleName("Mygwt-MenuItem");
		}
		
		{
			final MenuBar menuBarFaq = new MenuBar();
			horizontalPanel.add(menuBarFaq);

			final MenuBar menuBar_1 = new MenuBar(true);
			
			final MenuItem mFAQ1 = menuBar_1.addItem("» lista", new Command() {
				public void execute() {
					hpData.clear();
					hpData.add(new FaqList(1));
					lblSeccao.setText("FAQ :: Lista");
				}
			});
			mFAQ1.setStyleName("Mygwt-MenuSubItem");

			if(new Integer (this.getSession("Tipo").toString()).intValue() == 2)
			{
				final MenuItem mFAQ2 = menuBar_1.addItem("» adicionar", new Command() {
					public void execute() {
						hpData.clear();
						hpData.add(new FaqAdd());
						lblSeccao.setText("FAQ :: Adicionar");
					}
				});
				mFAQ2.setStyleName("Mygwt-MenuSubItem");
			}
			final MenuItem mFAQ = menuBarFaq.addItem("FAQ", menuBar_1);
			mFAQ.setStyleName("Mygwt-MenuItem");
		}

		{
			if(new Integer(getSession("Tipo").toString()).intValue() > 2) //adm
			{
				final MenuBar menuBarAdm = new MenuBar();
				horizontalPanel.add(menuBarAdm);
					
				final MenuBar menuBar_2 = new MenuBar(true);

				final MenuItem mAdmCrAdm = menuBar_2.addItem("» criar assistente", new Command() {
					public void execute() {
						hpData.clear();
						hpData.add(new UserAdd(new String("Assistente")));
						
						lblSeccao.setText("Administração :: Adicionar Assistente");
					}
				});
				mAdmCrAdm.setStyleName("Mygwt-MenuSubItem");

				final MenuItem mAdmCrAss = menuBar_2.addItem("» criar administrador", new Command() {
					public void execute() {
						hpData.clear();
						hpData.add(new UserAdd(new String("Administrador")));
						
						lblSeccao.setText("Administração :: Adicionar Administrador");
					}
				});
				mAdmCrAss.setStyleName("Mygwt-MenuSubItem");

				final MenuItem mAdm4 = menuBar_2.addItem("» faqs", new Command() {
					public void execute() {
						hpData.clear();
						hpData.add(new FaqList(2));
						lblSeccao.setText("Administração :: Administradores");
					}
				});
				mAdm4.setStyleName("Mygwt-MenuSubItem");




				final MenuItem mAdm = menuBarAdm.addItem("Administração", menuBar_2);
				if(new Integer (this.getSession("Tipo").toString()).intValue() == 4)
				{
					final MenuBar menuBar_4 = new MenuBar(true);
					
					final MenuItem mCEOg = menuBar_4.addItem("» geral", new Command() {
						public void execute()
						{
							hpData.clear();
							//TODO
							hpData.add(new PerformanceGeral());
							
							lblSeccao.setText("Administração :: Performance Geral");
						}
					});
					mCEOg.setStyleName("Mygwt-MenuSubItem");
	
					final MenuItem mAdm5 = menuBar_2.addItem("» performance", menuBar_4);
	
					final MenuItem mCEOa = menuBar_4.addItem("» assistente", new Command() {
						public void execute() 
						{
							hpData.clear();
							//TODO
							hpData.add(new PerformanceAssList());
							
							lblSeccao.setText("Administração :: Performance de Assistente");
						}
					});
					mCEOa.setStyleName("Mygwt-MenuSubItem");
					mAdm5.setStyleName("Mygwt-MenuSubItem");
					
				}
				mAdm.setStyleName("Mygwt-MenuItem");
			}

			if (new Integer(getSession("Tipo").toString()).intValue() > 2) //adm
			{
				final MenuBar menuBarSearch = new MenuBar();
				horizontalPanel.add(menuBarSearch);
	
				final MenuItem pesquisaMenuItem = menuBarSearch.addItem("Pesquisa", new Command() {
					public void execute() {
						hpData.clear();
						hpData.add(new UserSearch());
						lblSeccao.setText("Procura de Utilizadores");
					}
				});
				pesquisaMenuItem.setStyleName("Mygwt-MenuItem");
			}
		}

		final HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_5);
		horizontalPanel_5.setHeight("10px");

		final Label label_1 = new Label("");
		horizontalPanel_5.add(label_1);

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_3);
		horizontalPanel_3.setStyleName("loginPanel");
		horizontalPanel_3.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		horizontalPanel_3.setSpacing(6);

		final Label label = new Label("SECçÃO » ");
		horizontalPanel_3.add(label);
		label.setStyleName("section_title");


		horizontalPanel_3.add(lblSeccao);
		lblSeccao.setStyleName("section");
		
		dockPanel.add(hpData, DockPanel.CENTER);
		hpData.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		hpData.setSpacing(12);
		hpData.setWidth("100%");
		hpData.setStyleName("loginPanel");

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		dockPanel.add(horizontalPanel_4, DockPanel.SOUTH);
		horizontalPanel_4.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_4.setStyleName("gwt-MenuBar");
		horizontalPanel_4.setWidth("100%");
		horizontalPanel_4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final HTML html = new HTML("<table><tr><td height=\"18\" align=\"center\" valign=\"middle\" bgcolor=\"#333333\"><span class=\"developed\">Developed by: <a href=\"mailto:david.campos@ua.pt\" class=\"developedlink\">David Campos</a>, <a href=\"mailto:hugopicado@ua.pt\" class=\"developedlink\">Hugo Picado</a>, <a href=\"mailto:luisribeiro@ua.pt\" class=\"developedlink\">Luís Ribeiro</a>, <a href=\"pcalves@ua.pt\" class=\"developedlink\">Pedro Alves</a> [UA - DETI]</span></td></table></tr>");
		horizontalPanel_4.add(html);
		
		hpData.clear();
		hpData.add(new Inicio());
		lblSeccao.setText("Início");
		setWidth("100%");
	}
	
	/**
	 * Loads a composite to the main horizontal panel
	 * @param c Composite
	 */
	public static void loadComposite(Composite c)
	{
		hpData.clear();
		hpData.add(c);
	}
}
